/**
 * 系统资源添加js
 */
var comValidform=null;
var returnStatus=false;
var sysResourceAdd={
	dataCache:null,
		
	startEvent:function(){
		sysResourceAdd.gerAllResource();
		
		comValidform=$(".com-addForm").Validform({
			tiptype:2,
			tipSweep:true,
			beforeSubmit:function(curform){
				var parentId=sysResourceAdd.getSelData();
				if(parentId!=null){
					$('.com-addForm input[name="parentId"]').val(parentId);
				}
				
				var url = 'mgr/SysResourceCtrl/saveSysResource';
				var params = jsUtils.serializeJson($('.com-addForm'));
				var cb = function(data){
					alert(data.msg);
					//成功返回
					if(data.resultCode==200){
						//关闭窗口并刷新数据
						art.dialog.close();
						returnStatus=true;
					}
				};
				sendApiNoLoadSync(url, params, cb, 'POST');
				return false;
			}
		});
		
	},
	
	gerAllResource:function(){
		//获取所有资源
		var url ='mgr/SysResourceCtrl/getSysResourceList';
		var params={
			'curPage':0,
			'pageSize':9999
		};
		var cb = function(data){
			sysResourceAdd.dataCache=data.result;
		};
		sendApiNoLoadSync(url, params, cb, 'POST');
	},
	
	setCheck:function(){
		var zTree = $.fn.zTree.getZTreeObj("selSysResourceTree"),
		py = "",
		sy = "s",
		pn = "",
		sn = "s",
		type = { "Y":py + sy, "N":pn + sn};
		zTree.setting.check.chkboxType = type;
	},
	
	showTreeData:function(){
		var setting = {
			check: {
				enable: true,
				chkStyle: "radio",
				radioType: "all"
			},
			data: {
				simpleData: {
					enable: true
				}
			},
			view: {
				nameIsHTML: true
			}
		};
		var zNodes = [];
		var dataCache=sysResourceAdd.dataCache;
		for (var i = 0; i < dataCache.length; i++) {
			zNodes[i]={
				id:dataCache[i].id,
				name:dataCache[i].name,
				pId:dataCache[i].parentId
			};
			if(i==0){
				zNodes[i].open=true;
			}
		}
		$.fn.zTree.init($("#selSysResourceTree"), setting, zNodes);
		sysResourceAdd.setCheck();
		$("#py").bind("change", sysResourceAdd.setCheck());
		$("#sy").bind("change", sysResourceAdd.setCheck());
		$("#pn").bind("change",	sysResourceAdd.setCheck());
		$("#sn").bind("change",	sysResourceAdd.setCheck());
	},
	
	//选择返回事件
	getSelData:function(){
		var zTree = $.fn.zTree.getZTreeObj("selSysResourceTree");
		var nodes = zTree.getCheckedNodes();  // 取得所有选中的
		var len=nodes.length;
		if(len!=1){
			return null;
		}
		return nodes[0].id;
	},
	
	init:function(){
		sysResourceAdd.startEvent();
		sysResourceAdd.showTreeData();
	}
}
$(document).ready(function() {
	sysResourceAdd.init();
});
function submitForm(){
	comValidform.submitForm();
	return returnStatus;
}